package com.cupcorn.lib.game;

import org.andengine.entity.shape.RectangularShape;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.cupcorn.lib.Anchor;
import com.cupcorn.lib.Ratio;
import com.cupcorn.lib.Utils;
import com.cupcorn.lib.define.GameEngineConfiguration;

public class GameAnimatedSprite extends AnimatedSprite
{
	public GameRectangle	rectangle;
	public GameText			text;
	
	public GameRectangle	rectangle_debug[]	= new GameRectangle[4];
	
	public GameAnimatedSprite(float pX, float pY, float pWidth, float pHeight, ITiledTextureRegion pTiledTextureRegion,
		VertexBufferObjectManager pVertexBufferObjectManager)
	{
		super(pX, pY, pWidth, pHeight, pTiledTextureRegion, pVertexBufferObjectManager);
		
		if (GameEngineConfiguration.enable_trace)
		{
			// vertical line
			for (int i = 0; i < 2; i++)
			{
				rectangle_debug[i] = new GameRectangle(0, pY, pWidth, 3, pVertexBufferObjectManager);
			}
			
			// horizontal line
			for (int i = 2; i < rectangle_debug.length; i++)
			{
				rectangle_debug[i] = new GameRectangle(0, 0, 3, pHeight, pVertexBufferObjectManager);
			}
			
			for (int i = 0; i < rectangle_debug.length; i++)
			{
				rectangle_debug[i].setColor(1, 0, 0);
			}
			
			attachChild(rectangle_debug[0], 0, 0, Anchor.TOP_LEFT, Ratio.NONE);
			attachChild(rectangle_debug[1], 0, 0, Anchor.BOTTOM_LEFT, Ratio.NONE);
			attachChild(rectangle_debug[2], 0, 0, Anchor.TOP_LEFT, Ratio.NONE);
			attachChild(rectangle_debug[3], 0, 0, Anchor.TOP_RIGHT, Ratio.NONE);
		}
		
	}
	
	public void setPosition(float pX, float pY, Anchor anchor, Ratio ratio)
	{
		super.setPosition(Utils.getPosX(pX, this.mWidth, anchor, ratio), Utils.getPosY(pY, mHeight, anchor, ratio));
	}
	
	public void destroy()
	{
		this.detachSelf();
		this.getTiledTextureRegion().getTexture().unload();
	}
	
	public void addRectangle()
	{
		rectangle = new GameRectangle(0, 0, mWidth, mHeight, getVertexBufferObjectManager());
		attachChild(rectangle);
	}
	
	public void addRectangle(GameRectangle rectangle)
	{
		this.rectangle = rectangle;
		rectangle.setPosition(Utils.getCenter(mWidth, rectangle.getWidthScaled()), Utils.getCenter(mHeight, rectangle.getHeightScaled()));
		attachChild(rectangle);
	}
	
	public void addText(GameText text)
	{
		this.text = text;
		text.setPosition(Utils.getCenter(mWidth, text.getWidthScaled()), Utils.getCenter(mHeight, text.getHeightScaled()));
		attachChild(text);
	}
	
	public void addText(GameText text, float x, float y, Anchor anchor, Ratio ratio)
	{
		this.text = text;
		float _x = Utils.getPosX(x, text.getWidth(), anchor, ratio, this);
		float _y = Utils.getPosY(y, text.getHeight(), anchor, ratio, this);
		text.setPosition(_x, _y);
		attachChild(text);
	}
	
	public void attachChild(RectangularShape child, float x, float y, Anchor anchor, Ratio ratio)
	{
		attachChild(child);
		float _x = Utils.getPosX(x, child.getWidth(), anchor, ratio, this);
		float _y = Utils.getPosY(y, child.getHeight(), anchor, ratio, this);
		child.setPosition(_x, _y);
	}
	
}
